//
//  NSString+dynamicSize.m
//  Nooryes
//
//  Created by uuu9 on 2018/9/24.
//  Copyright © 2018年 UUU9. All rights reserved.
//

#import "NSString+dynamicSize.h"

@implementation NSString (dynamicWidth)

- (CGSize)dynamicWidthWithFontSize:(CGFloat)fontSize andCGSize:(CGSize)size andLineSpace:(NSInteger)space{
    NSMutableDictionary *attribute = [NSMutableDictionary dictionary];
    [attribute setValue:[UIFont systemFontOfSize:fontSize] forKey:NSFontAttributeName];
    if (space!=0) {
        NSMutableParagraphStyle *descStyle = [[NSMutableParagraphStyle alloc]init];
        [descStyle setLineSpacing:space];//行间
        [attribute setValue:descStyle forKey:NSParagraphStyleAttributeName];
    }
    NSStringDrawingContext *context = [[NSStringDrawingContext alloc] init];
    
    CGSize textSize = [self boundingRectWithSize:size
                                         options:NSStringDrawingUsesLineFragmentOrigin |NSStringDrawingUsesFontLeading
                                      attributes:attribute
                                         context:context].size;
    
    
    return textSize;
}

@end
